'''
The Route file reader

Created on 2016-1-24

@author: Soul
'''

route_map_array = []

class IpRouter():
    def __init__(self, filename):
        if filename:  # should add some validations here
            self.__filename__ = filename
            self._read_()
        else:
            return None

    def _read_(self):
        global route_map_array
        route_file = open(self.__filename__)
        lines = route_file.readlines()
        for line in lines:
            if line and line.strip() and (not line[0] == '#'):
                ip_pattern = "".join((line.split(','))[0])
                web_keyws = "".join((line.split(','))[1])
                web_keyws_list = web_keyws.split('|')
                web_tpls = "".join((line.split(','))[2])

                route_item = {'ip_pattern': ip_pattern, 'website_keywords': web_keyws_list, 'website_tpl': web_tpls}

                route_map_array.append(route_item)
        route_file.close()

    @staticmethod
    def getIp(domain):
        import socket
        ipaddr = socket.getaddrinfo(domain, 'http')[0][4][0]
        return ipaddr